package cn.module.component.common
{    
import cn.module.component.productManager.view.ProductsType_Module;

import flash.events.Event;
import flash.events.MouseEvent;

import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.controls.Image;
import mx.controls.Tree;
import mx.controls.treeClasses.TreeItemRenderer;
import mx.controls.treeClasses.TreeListData;
import mx.events.CloseEvent;
import mx.events.ItemClickEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
   
/** 
 * 在树的节点后面增加图标，点击后可达到增加、修改、删除节点等功能
 * 所有需要继承TreeItemRenderer
 * @author ken 
 */    
public class ProductsTypeInitTreeRenderer extends TreeItemRenderer    
{    
    public function ProductsTypeInitTreeRenderer()    
     {    
        super();    
     }    
        
    /** 
      * 设置所需图片
      */
     protected var addShape:Image;   //增
     protected var editShape:Image;  //改
     protected var delShape:Image;   //删
     
     protected var dataSource:XML;
        
    /** 
      * 构建Image 
      */         
     override protected function createChildren():void   
     {    
        super.createChildren();
        addShape = new Image();
        addShape.source = "./fleximages/treeListIcons/tree_add.png";
        addShape.toolTip = "增加下级单位";
        addChild(addShape);
        addShape.addEventListener(MouseEvent.CLICK, addChangeHandler);
        //修改和删除，根节点不需要
        dataSource = ((this.owner as Tree).dataProvider as XMLListCollection).source.parent();
        if(this.label.text != dataSource.elements("node")[0].attribute("label")){  
            editShape = new Image();  
            delShape = new Image();
            editShape.source = "./fleximages/treeListIcons/tree_edit.png";
            delShape.source = "./fleximages/treeListIcons/tree_del.png";
            editShape.toolTip = "修改单位";
            delShape.toolTip = "删除单位";
            addChild(editShape);
            addChild(delShape);
            editShape.addEventListener(MouseEvent.CLICK, editChangeHandler);
            delShape.addEventListener(MouseEvent.CLICK, delChangeHandler);
        }
     }    
        
    /** 
      * 点击图标时,更新dataProvider 
      * @param event 
      */         
    protected function addChangeHandler( event:Event ):void   
    {    
    	var tree:Tree=this.owner as Tree;
    	if(!tree.enabled){ //flase是不起作用，正被屏蔽
    		return;
    	}
    	var e:ItemClickEvent = new ItemClickEvent("treeimageclick",false,false,null,6,null,null);  //伪造新增点击，index=6 和index=0对应
    	(this.owner.parent.parent.parent as ProductsType_Module).treeImageClickflag = 1; //点击了目录树上的修改
       (this.owner.parent.parent.parent as ProductsType_Module).buttonClick(e);
    }
    protected function editChangeHandler( event:Event ):void   
    {    
    	var tree:Tree=this.owner as Tree;
    	if(!tree.enabled){ //flase是不起作用，正被屏蔽
    		return;
    	}
    	var e:ItemClickEvent = new ItemClickEvent("treeimageclick",false,false,null,7,null,null);  //伪造修改点击，index=1
       (this.owner.parent.parent.parent as ProductsType_Module).treeImageClickflag = 2; //点击了目录树上的修改
       (this.owner.parent.parent.parent as ProductsType_Module).buttonClick(e);
    }
    protected function delChangeHandler( event:Event ):void   
    {   
    	var tree:Tree=this.owner as Tree;
    	if(!tree.enabled){ //flase是不起作用，正被屏蔽
    		return;
    	}
    	var e:ItemClickEvent = new ItemClickEvent("treeimageclick",false,false,null,8,null,null);  //伪造删除点击，index=2
    	(this.owner.parent.parent.parent as ProductsType_Module).treeImageClickflag = 3; //点击了目录树上的删除
       (this.owner.parent.parent.parent as ProductsType_Module).buttonClick(e);
    }
       
    /** 
      * 初始化控件时, 给checkbox赋值 
      */
    override protected function commitProperties():void  
     {
       super.commitProperties();  
//       Alert.show("123456");
     }
             
    /** 
      * 重置itemRenderer的宽度 
      */         
     override protected function measure():void
     {    
        super.measure();    
        measuredWidth += addShape.getExplicitOrMeasuredWidth();
//        measuredWidth += editShape.getExplicitOrMeasuredWidth();
//        measuredWidth += delShape.getExplicitOrMeasuredWidth(); 
     }    
        
    /** 
      * 重新排列树中节点位置, 将label后移 ;显示增删改按钮
      * @param unscaledWidth 
      * @param unscaledHeight 
      */         
     override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void   
     {    
        super.updateDisplayList(unscaledWidth, unscaledHeight);    
         var startx:Number = data ? TreeListData( listData ).indent : 0;    
            
        //disclosureIcon : Class
        //A class representing the disclosure icon for the item in the Tree control.在此渲染器中显示泄露图标的内部 IFlexDisplayObject
        //IFlexDisplayObject 接口定义外观元素的接口（父项图标）
        if (disclosureIcon)    
         {
             disclosureIcon.x = startx;    

             startx = disclosureIcon.x + disclosureIcon.width;    
 
             disclosureIcon.setActualSize(disclosureIcon.width,    
                                          disclosureIcon.height);    
                 
             disclosureIcon.visible = data ?    
                                      TreeListData( listData ).hasChildren :    
                                     false;    
         }    
        
        //A class representing the icon for the item in the Tree control.在此渲染器中显示图标的内部 IFlexDisplayObject（叶子图标）
        if (icon)    
         {    
             icon.x = startx - 24;   //茎的项目由于去掉了文件夹，label项目做了左移，所有叶子也要左移，这里先移动叶子图标 
             startx = icon.x + icon.measuredWidth;    
             icon.setActualSize(icon.measuredWidth, icon.measuredHeight);
             label.x = icon.x + 24;
         }    
//         
//         delShape.move(unscaledWidth-delShape.width, ( unscaledHeight - delShape.height ) / 2 );
//         delShape.setActualSize(delShape.measuredWidth, delShape.measuredHeight);
         //14 * this.label.length是设置增改删的左右位置的,每个字14，length是个数,28是不要据字太近
         addShape.move(label.x + (14 * this.label.length) + 14, ( unscaledHeight - addShape.height ) / 2 );
         addShape.setActualSize(16, 16);  //measuredHeight属性使用时，会出现图标与行不对齐的现象，当鼠标移到节点上才会重新对齐
         dataSource = ((this.owner as Tree).dataProvider as XMLListCollection).source.parent();
         if(this.label.text != dataSource.elements("node")[0].attribute("label")){
         	 //非根目录需要修改、删除按钮
             editShape.move(addShape.x + 20, ( unscaledHeight - editShape.height ) / 2 );
             delShape.move(editShape.x + 20, ( unscaledHeight - delShape.height ) / 2 );
             editShape.setActualSize(16, 16);
             delShape.setActualSize(16, 16);
         }
       }    
    }    
}   
